# encoding: utf-8
module SceneManager
  #--------------------------------------------------------------------------
  # * Get First Scene Class
  #--------------------------------------------------------------------------
  def self.first_scene_class
    Scene_OJVA
  end
end

class Scene_OJVA < Scene_Base
  def start
    OJ.start { OJ.main }
  end

  def update_basic
    Graphics.update
    OJ.update
  end

  def terminate
  end
end

module OJ
  Time_Limit = (1000 / Graphics.frame_rate - 1) / 1000.0

  def OJ.update
    @t = Time.now
    nil until @fiber_tree.update { |leaf, leaf_ix|
      if Time.now - @t >= Time_Limit
        puts "time out: %.3f, %d of %d" % [Time.now - @t - Time_Limit, leaf_ix, @fiber_tree.leafs.size]
        Graphics.update
        @t = Time.now
      end
    }
    # nil until @fiber_tree.update
    if @fiber_tree.leafs.empty?
      # no fibers, use leafs.clear to force flush all fibers
      SceneManager.return
    end
  end
end
